/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package classes;

/**
 *
 * @author cardodav
 */

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;


public class BancoDeDados {    
    
        private Connection conn = null;
        private Statement stm = null;
        
        public BancoDeDados(){
        
            try{
                if (!this.isConectado()){
                    this.conn = this.conectar();
                }   
            } catch(Exception ex){
                System.out.println("Erro ao criar objeto BancoDeDados");
            }            
        }
    
        Connection conectar(){
            
            String ConnectionString = "jdbc:oracle:thin:@localhost:1521:XE";
            try {
                Class.forName("oracle.jdbc.driver.OracleDriver");
                DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
                conn = DriverManager.getConnection(ConnectionString,"vetlion","vetlion");
                System.out.println("Conexão criada com sucesso.");
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(BancoDeDados.class.getName()).log(Level.SEVERE, null, ex);
                System.out.println("Erro ao conectar o driver de BD");
            } catch (SQLException ex ){
                System.out.println("Erro ao conectar no banco");
            }
            return conn;
        }
        
        public void desconectar(){
            try{
                if (this.conn != null && !conn.isClosed()){
                    this.conn.close();
                    System.out.println("Conexão fechada com sucesso");               
                }            
            } catch( SQLException ex){
                System.out.println("Erro co fechar conexão com o BD");           
            }        
        }
        
        private boolean isConectado(){

            boolean ret = false;
            
            try{
                if (this.conn != null && !conn.isClosed()){
                    ret = true;
                }else{
                    ret = false;
                }                
            } catch (SQLException ex){
                System.out.println("Erro ao verificar se o Banco de Dados está conectado");           
                
            }            
            return ret;        
        }
        
        public ResultSet consultar(String query ){           
            
            ResultSet rs = null;
            try             {
                Statement stm = conn.createStatement();
                rs = stm.executeQuery(query);                
            } catch ( Exception ex){
                System.out.println("Erro ao retornar o resultado da query: " + query);
            }
            return rs;            
        }
    
        public int executarDML(String query){
        
            int ret = -1;
            
            try{
                stm = conn.createStatement();
                stm.execute(query);
                ret = stm.getUpdateCount();
            } catch(SQLException ex)
            {
                System.out.println("Erro ao retornar o resultado da query: " + query);            
            }            
            
            return ret;
        }
    
}
